//
//  EventDetailView.m
//  eticket
//
//  Created by AnhQuocNguyen on 4/18/13.
//  Copyright (c) 2013 SoinMedia. All rights reserved.
//

#import "EventDetailView.h"

@implementation EventDetailView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      
    }
    return self;
}

- (void)dealloc {
    [_btnBack release];
    SAFE_RELEASE_OBJECT(mHud);
    [_lbTitle release];
    [_lbCategory release];
    [_lbStartTime release];
    [_lbAddress release];
    [_descriptionView release];
    [_txtDescription release];
    [_imgViewMap release];
    [_btnBuyTicket release];
    [_lbBuyTicket release];
    [_lbDescription release];
    [super dealloc];
}

- (void)setupEventDetailView{
    _descriptionView.layer.borderWidth = 1;
    _descriptionView.layer.borderColor = [[UIColor colorWithRed:154/255 green:154/255 blue:154/255 alpha:0.1] CGColor];
    _descriptionView.layer.cornerRadius = 3;
    _descriptionView.hidden = YES;

    _lbAddress.hidden = YES;
    _lbCategory.hidden = YES;
    _lbStartTime.hidden = YES;
    _lbTitle.hidden = YES;
    _txtDescription.hidden = YES;
    _imgViewMap.hidden = YES;
    _btnBuyTicket.hidden = YES;
    _lbBuyTicket.hidden = YES;
    _lbDescription.hidden = YES;
}

- (void)getDataFromServer{
    mLoading = YES;
    [self initHUD:@"Loading...."];
    [mHud showWhileExecuting:@selector(waitting) onTarget:self withObject:nil animated:NO];

    
    dispatch_async(kBgQueue, ^{
        /////////////////////////////////////////////////////////////////
        NSString *urlString = [NSString stringWithFormat:@"%@%@%@/%@", URL_island_API, events_API, _eventSlug, TOKEN__island];
        NSURL *url = [NSURL URLWithString:urlString];
        
        __block ASIFormDataRequest *requestServer = [ASIFormDataRequest requestWithURL:url];
        [requestServer setTimeOutSeconds:20];
        [requestServer setCompletionBlock:^{
            mLoading = NO;
            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:[requestServer responseData] options:kNilOptions error:&error];
            NSLog(@"%@", jsonData);
            
            //Check error server
            if (error) {
//                NSLog(@"%@", [error description]);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error description] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                [alert show];
                SAFE_RELEASE_OBJECT(alert);
                return;
            }
            
            
            //Check error json
            NSDictionary *jsonError = [jsonData objectForKey:@"error"];
            if ([[jsonError objectForKey:@"code"] intValue] != 200) {
                NSArray *messageArray = [jsonError objectForKey:@"message"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[messageArray objectAtIndex:0] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
                [alert show];
                SAFE_RELEASE_OBJECT(alert);
                return;
            }
        //////////////////////////////////////
        NSDictionary *jsonDataObject = [jsonData objectForKey:@"data"];
        NSDictionary *jsonEvent = [jsonDataObject objectForKey:@"event"];

        //Get Category
        NSDictionary *jsonCategory = [jsonEvent objectForKey:@"categories"];
        NSDictionary *jsonPrimary = [jsonCategory objectForKey:@"primary"];

        //Get Thumbnail
//        NSDictionary *jsonThumbnail = [jsonEvent objectForKey:@"thumbnail"];
//        NSDictionary *jsonDetail = [jsonThumbnail objectForKey:@"detail"];
//        NSString *thumbnailURL = [jsonDetail objectForKey:@"filename"];
            
        NSString *category = [jsonPrimary objectForKey:@"title"];
        NSString *address = [jsonEvent objectForKey:@"address"];
        NSString *startTime = [jsonEvent objectForKey:@"start_time"];
        NSString *title = [jsonEvent objectForKey:@"title"];
        NSString *description = [Utilities stringByStrippingHTML:[jsonEvent objectForKey:@"description"]];
            
        //Set label
        _lbTitle.text = title;
        _lbCategory.text = category;
        _lbStartTime.text = [[startTime componentsSeparatedByString:@" "] objectAtIndex:0];
        _lbAddress.text = address;
        _txtDescription.text = description;
            
        //Set display
        _descriptionView.hidden = NO;
        _lbAddress.hidden = NO;
        _lbCategory.hidden = NO;
        _lbStartTime.hidden = NO;
        _lbTitle.hidden = NO;
        _txtDescription.hidden = NO;
        _imgViewMap.hidden = NO;
        _btnBuyTicket.hidden = NO;
        _lbBuyTicket.hidden = NO;
        _lbDescription.hidden = NO;
        }];
        
        [requestServer setFailedBlock:^{
            mLoading = NO;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Loading faild !!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alert show];
            SAFE_RELEASE_OBJECT(alert);
        }];
        [requestServer startSynchronous];
    });
}

////////////////////////////////////////////////////////////////////////
#pragma mark - btnEventDetailBack_Clicked
- (IBAction)btnEventDetailBack_Clicked:(id)sender{
    if ([_eventDetailViewDelegate respondsToSelector:@selector(eventDetailViewWithback:)]) {
        [_eventDetailViewDelegate eventDetailViewWithback:self];
    }
}

#pragma mark - UIAlertViewDegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        if ([_eventDetailViewDelegate respondsToSelector:@selector(eventDetailViewWithback:)]) {
            [_eventDetailViewDelegate eventDetailViewWithback:self];
        }
    }
}

#pragma mark - MBProgressHUD
-(void)initHUD:(NSString *)alert {
    if (mHud) {
        [mHud removeFromSuperview];
        SAFE_RELEASE_OBJECT(mHud);
    }
    mHud = [[MBProgressHUD alloc] initWithView:self];
    //    mHud.delegate = self;
    mHud.labelText = alert;
    mHud.dimBackground = YES;
    [self addSubview:mHud];
    
}
-(void)waitting{
    while (mLoading) {
        usleep(5000);
        if (_countTimeout >= 187*20 /* 187 ~ 3s ==>> 187 *20 ~ 1' */) {
            mLoading = NO;
            
        }
    }
}
@end
